package com.sky.controller.admin;


import com.sky.constant.MessageConstant;
import com.sky.constant.RedisDataBaseNameConstant;
import com.sky.exception.ShopStatusException;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "店铺相关接口")
@RestController(value = "AdminShopController")
@RequestMapping("/admin/shop")
public class ShopController {
    //这里不需要指定泛型，因为RedisTemplate默认的序列化器是StringRedisSerializer
    @Autowired
    private  RedisTemplate<String,Object> redisTemplate;

    /**
     * 切换店铺状态
     * @param status 店铺状态，1为营业，0为关闭
     * @return
     */
    @ApiOperation("切换店铺状态")
    @PutMapping("/{status}")
    public Result<String> setStatus(@PathVariable @ApiParam("店铺状态，1为营业，0为关闭") Integer status) {
        redisTemplate.opsForValue().set(RedisDataBaseNameConstant.SHOP_STATUS_KEY,status);
        log.info("切换店铺状态：{}", status == 1?"营业":"关闭");
        return Result.success();
    }

    /**
     * 获取店铺状态
     * @return 店铺状态，1为营业，0为关闭
     */
    @ApiOperation("获取店铺状态")
    @GetMapping("/status")
    public Result<Integer> getStatus() {
        Integer status = (Integer) redisTemplate.opsForValue().get(RedisDataBaseNameConstant.SHOP_STATUS_KEY);
        if(status == null){
            throw new ShopStatusException(MessageConstant.SHOP_STATUS_EXCEPTION);
        }
        log.info("获取店铺状态：{}", status == 1?"营业":"关闭");
        return Result.success(status);
    }
}
